/*SpikeParser class
 * @author Charles Schuh
 * 
 * SpikeParser takes a text file containing a list of times (in ms)
 * and the IDs of neurons and places these pairs into a HashMap. HashMap's
 * key will be the long value produced by multiplying the time by 10
 * and the associated value will be the ID of the neuron that's spiking.
 */
package neuronvisualizer;
import java.io.*;
import java.util.StringTokenizer;
import java.util.HashMap;
public class SpikeParser {
	//Parses text file in directory dir into a HashMap of spikes
	public static HashMap<Long,Integer> parse(String dir) throws IOException{
		HashMap<Long,Integer> h = new HashMap<Long,Integer>();
		File readfrom = new File(dir);
		BufferedReader r = new BufferedReader(new FileReader(readfrom));
		StringTokenizer st;
		String curline = r.readLine();
		//Main loop: processes all lines of input into spikes and
		//adds them to the HashMap
		while(curline != null){
			st = new StringTokenizer(curline);
			//Multiplies the floating point value by 10 and
			//converts it to Long so it may be used as the key of
			//the HashMap
			long k = (new Float(10*(Float.parseFloat(st.nextToken())))).longValue();
			int v = Integer.parseInt(st.nextToken());
			//Adds the new spike to the HashMap
			h.put(k, v);
			curline = r.readLine();
		}
		return h;
	}
}
